package com.nxk.modules.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.core.dao.SysMenuDao;
import com.nxk.modules.core.entity.BaseEntity;
import com.nxk.modules.core.entity.SysMenu;
import com.nxk.modules.core.service.SysMenuService;
import com.nxk.modules.exception.CustomException;
import com.nxk.modules.exception.RequestStatus;
import com.nxk.modules.utils.ListToJson;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    /**
     * 保存数据,不做保存状态
     *
     * @param sysMenu
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean save(SysMenu sysMenu) {
        if (StringUtils.isNotBlank(sysMenu.getId())) {
            return super.updateById(sysMenu);
        }
        if (StringUtils.isBlank(sysMenu.getParentId())){
            sysMenu.setParentId("0");
        }
        return super.save(sysMenu);
    }

    /**
     * 只负责保存状态
     *
     * @param sysMenu
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean updateStatus(SysMenu sysMenu) {
        //如果是启用,直接启用
        if (sysMenu.getStatus().equals(BaseEntity.STATUS_NORMAL)) {
            super.updateById(sysMenu);
        }
        //如果是停用也停用
        if (sysMenu.getStatus().equals(BaseEntity.STATUS_STOP)) {
            List<String> idList = new ArrayList<>();
            //查询所有子菜单id
            LambdaQueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>().lambda()
                    .eq(SysMenu::getParentId, sysMenu.getId()).select(SysMenu::getId);
            super.list(queryWrapper).stream().forEach(entity -> {
                idList.add(entity.getId());
            });
            //添加id
            idList.add(sysMenu.getId());
            //全部停用
            sysMenuDao.updateBatchStatus(idList, sysMenu.getStatus());
        }
        //如果是删除,就物理删除
        if (sysMenu.getStatus().equals(BaseEntity.STATUS_DELETE)){
             List<String> idList = new ArrayList<>();
            //查询所有子菜单id
            LambdaQueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>().lambda()
                    .eq(SysMenu::getParentId, sysMenu.getId()).select(SysMenu::getId);
            super.list(queryWrapper).stream().forEach(entity -> {
                idList.add(entity.getId());
            });
            //添加id
            idList.add(sysMenu.getId());
            super.removeByIds(idList);
        }
        return true;
    }


    /**
     * 查询所有菜单,区分父子级别,返回正确的json格式给前端
     *
     * @param sysMenu
     * @return
     */
    @Override
    public List<SysMenu> list(SysMenu sysMenu) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        //queryWrapper.setEntity(sysMenu);
        if (StringUtils.isNotBlank(sysMenu.getMenuName())){
            queryWrapper.lambda().like(SysMenu::getMenuName,sysMenu.getMenuName());
        }
        queryWrapper.orderByAsc("sort").in("status",1,2);;
        List<SysMenu> list = super.list(queryWrapper);
        return ListToJson.getMenuJson(list);
    }



    /**
     * 查询所有菜单,区分父子级别,返回正确的json格式给前端
     *
     * @param sysMenu
     * @return
     */
    @Override
    public List<SysMenu> listRole(SysMenu sysMenu) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        //queryWrapper.setEntity(sysMenu);
        if (StringUtils.isNotBlank(sysMenu.getMenuName())){
            queryWrapper.lambda().like(SysMenu::getMenuName,sysMenu.getMenuName());
        }
        queryWrapper.orderByAsc("sort").in("status",1,2);;
        List<SysMenu> list = super.list(queryWrapper);
        return ListToJson.getMenuJson2(list);
    }

    /**
     * 根据夫菜单id,返回所有子菜单
     *
     * @param parentId
     * @return
     */
    @Override
    public List<SysMenu> getChildren(String parentId,String status) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId,parentId);
        return super.list(queryWrapper);
    }


    /**
     * 根据id获取单条数据
     *
     * @param id
     * @return
     */
    @Override
    public SysMenu getById(String id) {
        return super.getById(id);

    }


    /**
     * 根据id物理删除菜单数据
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
        //获取当前要删除父菜单
        SysMenu byId = super.getById(id);
        //获取夫菜单的所有子菜单,因为是逻辑删除,不需要加status
        LambdaQueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, id).select(SysMenu::getId);
        if (super.count(queryWrapper) > 0) {
            throw new CustomException(Integer.valueOf(String.valueOf(RequestStatus.ERROR_REFUSE)), "当前要删除的菜单有正常使用的子菜单,不能删除");
        }
        return super.removeById(id);
    }

}
